﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Storage;
using Windows.Storage.Streams;
using System.IO;
using System.Runtime.Serialization;
using Windows.UI.Xaml.Media.Imaging;

namespace eLuk.Common
{
	public class Misc
	{
		public static BitmapImage GetBitmap( byte[] bytes, int width, int height )
		{
			using ( InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream() )
			{
				using ( DataWriter writer = new DataWriter( stream.GetOutputStreamAt( 0 ) ) )
				{
					writer.WriteBytes( bytes );
					writer.StoreAsync().GetResults();
				}

				BitmapImage bimage = new BitmapImage();
				bimage.DecodePixelWidth = width;
				bimage.DecodePixelWidth = height;
				bimage.SetSource( stream );
				return bimage;
			}
		}

		public static BitmapImage GetBitmap( string file, int width, int height )
		{
			BitmapImage bimage = new BitmapImage( new Uri( "ms-appx:///Assets/user.png" ) );
			return bimage;
		}

		public static BitmapImage GetDefaultProfilePicture()
		{
			BitmapImage bimage = new BitmapImage( new Uri( "ms-appx:///Assets/user.png" ) );
			return bimage;
		}

		//public void ResetMainAppBar( eLukMainView rootPage )
		//{
		//	// Save original AppBar so we can restore it afterward
		//	AppBar originalAppBar = rootPage.BottomAppBar;

		//	// Use a CommandBar rather than an AppBar so that we get default layout
		//	CommandBar commandBar = new CommandBar();

		//	// Create the 'Add' button
		//	AppBarButton add = new AppBarButton();
		//	add.Label = "Add";
		//	add.Icon = new SymbolIcon( Symbol.Add );

		//	commandBar.PrimaryCommands.Add( add );

		//	// Create the 'Remove' button
		//	AppBarButton remove = new AppBarButton();
		//	remove.Label = "Remove";
		//	remove.Icon = new SymbolIcon( Symbol.Remove );

		//	commandBar.PrimaryCommands.Add( remove );

		//	commandBar.PrimaryCommands.Add( new AppBarSeparator() );

		//	// Create the 'Delete' button
		//	AppBarButton delete = new AppBarButton();
		//	delete.Label = "Delete";
		//	delete.Icon = new SymbolIcon( Symbol.Delete );

		//	commandBar.PrimaryCommands.Add( delete );

		//	// Create the 'Camera' button
		//	AppBarButton camera = new AppBarButton();
		//	camera.Label = "Camera";
		//	camera.Icon = new SymbolIcon( Symbol.Camera );
		//	commandBar.SecondaryCommands.Add( camera );

		//	// Create the 'Bold' button
		//	AppBarButton bold = new AppBarButton();
		//	bold.Label = "Bold";
		//	bold.Icon = new SymbolIcon( Symbol.Bold );
		//	commandBar.SecondaryCommands.Add( bold );

		//	// Create the 'Italic' button
		//	AppBarButton italic = new AppBarButton();
		//	italic.Label = "Italic";
		//	italic.Icon = new SymbolIcon( Symbol.Italic );
		//	commandBar.SecondaryCommands.Add( italic );

		//	// Create the 'Underline' button
		//	AppBarButton underline = new AppBarButton();
		//	underline.Label = "Underline";
		//	underline.Icon = new SymbolIcon( Symbol.Underline );
		//	commandBar.SecondaryCommands.Add( underline );

		//	// Create the 'Align Left' button
		//	AppBarButton left = new AppBarButton();
		//	left.Label = "Align Left";
		//	left.Icon = new SymbolIcon( Symbol.AlignLeft );
		//	commandBar.SecondaryCommands.Add( left );

		//	// Create the 'Align Center' button
		//	AppBarButton center = new AppBarButton();
		//	center.Label = "Align Center";
		//	center.Icon = new SymbolIcon( Symbol.AlignCenter );
		//	commandBar.SecondaryCommands.Add( center );

		//	// Create the 'Align Right' button
		//	AppBarButton right = new AppBarButton();
		//	right.Label = "Align Right";
		//	right.Icon = new SymbolIcon( Symbol.AlignRight );
		//	commandBar.SecondaryCommands.Add( right );

		//	rootPage.BottomAppBar = commandBar;
            
			
		//}


		//public static async Task<bool> WriteeLukGroup( eLuk.Entity.eLukComponent group )
		//{

		//	if ( group == null )
		//		return false;

		//	bool result = true;

		//	StorageFolder folder = null;
		//	bool createFolder = false;
		//	bool createFile = false;
		//	StorageFile file = null;

		//	try
		//	{
		//		try
		//		{
		//			folder = await ApplicationData.Current.LocalFolder.GetFolderAsync( "eLuk" );
		//			createFolder = false;
		//		}
		//		catch ( Exception ex )
		//		{
		//			if ( ex is FileNotFoundException )
		//				createFolder = true;
		//		}
		//		if ( createFolder )
		//			folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync( "eLuk" );

		//		if ( folder != null )
		//		{
		//			try
		//			{
		//				file = await folder.GetFileAsync( "eLuk.eLuk" );
		//				await file.DeleteAsync( StorageDeleteOption.Default );
		//				createFile = true;
		//			}
		//			catch ( Exception ex )
		//			{
		//				if ( ex is FileNotFoundException )
		//					createFile = true;
		//			}
		//		}

		//		if ( createFile )
		//			file = await folder.CreateFileAsync( "eLuk.eLuk" );

		//		if ( file.IsAvailable )
		//		{
		//			using ( Stream fileStream = await file.OpenStreamForWriteAsync() )
		//			{
		//				MemoryStream memory = await Misc.SerializeAndProtect( group );
		//				memory.Seek( 0, SeekOrigin.Begin );
		//				await memory.CopyToAsync( fileStream );
		//				await fileStream.FlushAsync();
		//			}
		//		}
		//	}
		//	catch ( Exception ex )
		//	{

		//	}

		//	return result;

		//}

		//public static async Task<eLuk.Entity.eLukComponent> ReadIMAPFolder()
		//{

		//	StorageFolder folder = null;
		//	StorageFile file = null;

		//	try
		//	{
		//		try
		//		{
		//			folder = ApplicationData.Current.LocalFolder.GetFolderAsync( "eLuk" ).GetResults();
		//		}
		//		catch ( FileNotFoundException )
		//		{
		//			return null;
		//		}
		//		catch ( Exception ex )
		//		{
		//			return null;
		//		}

		//		if ( folder == null )
		//			folder = ApplicationData.Current.LocalFolder.GetFolderAsync( "eLuk" ).GetResults();

		//		if ( folder != null )
		//		{
		//			file = await folder.GetFileAsync( "eLuk.eLuk" );
		//			using ( IInputStream stream = await file.OpenSequentialReadAsync() )
		//			{
		//				return await Misc.UnprotectAndSerialize( stream );
		//			}
		//		}
		//		else
		//		{
		//			return null;
		//		}
		//	}
		//	catch ( FileNotFoundException fnf )
		//	{
		//		return null;
		//	}
		//	catch ( Exception ex )
		//	{
		//		throw new Exception( "Unable to load the eLuk email object." );
		//	}
		//}

	}

}
